package com.lcf.app.awss3.controller;

import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.lcf.app.awss3.service.AmazonS3Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "AmazonS3-案例")
@RestController
@RequestMapping("AmazonS3")
@RequiredArgsConstructor
public class AmazonS3Controller {

    private final AmazonS3Service amazonS3Service;

    @Operation(summary = "文件上传")
    @Parameters({
            @Parameter(name = "file", description = "文件", in = ParameterIn.QUERY)
    })
    @PostMapping("upload")
    public Map<String, Object> upload(MultipartFile file) throws IOException {
        String key = amazonS3Service.putObject(file);
        return result(200, "ok", key);
    }

    @Operation(summary = "文件下载")
    @Parameters({
            @Parameter(name = "key", description = "文件名称", in = ParameterIn.QUERY)
    })
    @GetMapping("download")
    public void download(String key, HttpServletResponse response) throws IOException {
        S3ObjectInputStream stream = amazonS3Service.getObject(key);
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(key, "UTF-8"));
        IOUtils.copy(stream, response.getOutputStream());
    }

    @Operation(summary = "分片上传-初始化：返回uploadId")
    @GetMapping("initUpload")
    public Map<String, Object> initUpload(String filename) {
        String initUpload = amazonS3Service.initUpload(filename, null);
        return result(200, "ok", initUpload);
    }

    @Operation(summary = "分片上传-上传接口")
    @PostMapping("uploadPart")
    public Map<String, Object> uploadPart(@RequestPart("file") MultipartFile file,
                                          @RequestPart("uploadId") String uploadId,
                                          @RequestPart("number") String number) throws IOException {
        amazonS3Service.uploadPart(file, uploadId, Integer.parseInt(number));
        return result(200, "ok");
    }

    @Operation(summary = "分片上传-完成上传")
    @GetMapping("completeUpload")
    public Map<String, Object> completeUpload(@RequestParam("uploadId") String uploadId) {
        amazonS3Service.completeUpload(uploadId);
        return result(200, "ok");
    }


    private Map<String, Object> result(int code, String message) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("message", message);
        map.put("data", null);
        return map;
    }

    private Map<String, Object> result(int code, String message, Object data) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("message", message);
        map.put("data", data);
        return map;
    }

}
